package com.ruoyi.web.controller.ycdj;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.TUnit;
import com.ruoyi.system.service.TUnitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags="单位接口")
@RestController
@RequestMapping("unit")
public class UnitController extends BaseController {

    @Autowired
    public TUnitService tUnitService;

    //列表
    @ApiOperation("单位列表")
    @GetMapping("getlist")
    public AjaxResult getlist(@RequestParam(value="pageNum",defaultValue = "")Integer pageNum,
                              @RequestParam(value="pageSize",defaultValue = "")Integer pageSize,
                              @RequestParam(value="unitstatus",defaultValue = "")Integer unitstatus){

        QueryWrapper<TUnit>tUnitQueryWrapper=new QueryWrapper<>();
        if(unitstatus!=null){
            tUnitQueryWrapper.eq("unitstatus",unitstatus);
        }
        if(pageNum!=null){
            PageHelper.startPage(pageNum,pageSize);
            List<TUnit>unitList=tUnitService.list(tUnitQueryWrapper);
            PageInfo<TUnit>pageInfo=new PageInfo<>(unitList);
            return AjaxResult.success(pageInfo);
        }else{
            List<TUnit>unitList=tUnitService.list(tUnitQueryWrapper);
            return AjaxResult.success(unitList);
        }

    }

    //新增单位
    @ApiOperation("新增")
    @PostMapping("add")
    public AjaxResult add(@RequestBody TUnit tUnit){
        //检查重复
        QueryWrapper<TUnit>tUnitQueryWrapper=new QueryWrapper<>();
        tUnitQueryWrapper.eq("unitname",tUnit.getUnitname());
        tUnitQueryWrapper.eq("unittypeid",tUnit.getUnittypeid());
        List<TUnit>tUnitList=tUnitService.list(tUnitQueryWrapper);
        if(!tUnitList.isEmpty()){
            return AjaxResult.warn("单位已存在");
        }
        return toAjax(tUnitService.save(tUnit));
    }

    //编辑
    @ApiOperation("编辑")
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody TUnit tUnit){
        QueryWrapper<TUnit>tUnitQueryWrapper=new QueryWrapper<>();
        tUnitQueryWrapper.eq("unitname",tUnit.getUnitname());
        tUnitQueryWrapper.eq("unittypeid",tUnit.getUnittypeid());
        tUnitQueryWrapper.eq("unitorgid",tUnit.getUnitorgid());
        tUnitQueryWrapper.eq("transmember",tUnit.getTransmember());
        tUnitQueryWrapper.eq("unitstatus",tUnit.getUnitstatus());
        TUnit tUnit1=tUnitService.getOne(tUnitQueryWrapper);
        if(tUnit1!=null){
            if(tUnit1.getId().equals(tUnit.getId())){
                return AjaxResult.warn("名称重复");
            }
        }
        return toAjax(tUnitService.saveOrUpdate(tUnit));
    }

    //删除
    @ApiOperation("删除")
    @GetMapping("delete")
    public AjaxResult delete(Integer id){
        TUnit tUnit=tUnitService.getById(id);
        tUnit.setUnitstatus(0);
        return toAjax(tUnitService.saveOrUpdate(tUnit));
    }

    //删除
    @ApiOperation("启用")
    @GetMapping("open")
    public AjaxResult open(Integer id){
        TUnit tUnit=tUnitService.getById(id);
        tUnit.setUnitstatus(1);
        return toAjax(tUnitService.saveOrUpdate(tUnit));
    }

}
